package huaweiod.first;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 华为OD机试真题 Java 实现【处理器问题/ 高性能AI处理器】【2022.11 Q4 新题】
 */
public class A_2023_17_ProcessProblem {

    public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        Integer[] cores = Arrays.stream(in.nextLine().split("[\\[\\]\\,\\s]"))
                .filter(str -> !"".equals(str))
                .map(Integer::parseInt)
                .toArray(Integer[]::new);

        int target = in.nextInt();

        //初始化两个链路剩余可用的处理器
        ArrayList<Integer> processors_1 = new ArrayList<>();
        ArrayList<Integer> processors_2 = new ArrayList<>();

        // process
        Arrays.sort(cores);
        for (int i = 0; i < cores.length; i++) {
            if (cores[i] < 4) {
                processors_1.add(cores[i]);
            } else {
                processors_2.add(cores[i]);
            }
        }
        int size1 = processors_1.size();
        int size2 = processors_2.size();
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        switch (target) {
            case 1:
                if (size1 == 1 || size2 == 1) {
                    if(size1 == 1) dfs(0,1,processors_1,new ArrayList<Integer>(),result);
                    if(size2 == 1) dfs(0,1,processors_2,new ArrayList<Integer>(),result);
                } else if(size1 == 3 || size2 == 3){
                    if(size1 == 3) dfs(0,1,processors_1,new ArrayList<Integer>(),result);
                    if(size2 == 3) dfs(0,1,processors_2,new ArrayList<Integer>(),result);
                } else if(size1 == 2 || size2 == 2){
                    if(size1 == 2) dfs(0,1,processors_1,new ArrayList<Integer>(),result);
                    if(size2 == 2) dfs(0,1,processors_2,new ArrayList<Integer>(),result);
                } else if(size1 == 4 || size2 == 4) {
                    if(size1 == 4) dfs(0,1,processors_1,new ArrayList<Integer>(),result);
                    if(size2 == 4) dfs(0,1,processors_2,new ArrayList<Integer>(),result);
                }
                break;
            case 2:
                if (size1 == 2 || size2 == 2) {
                    if(size1 == 2) dfs(0,2,processors_1,new ArrayList<Integer>(),result);
                    if(size2 == 2) dfs(0,2,processors_2,new ArrayList<Integer>(),result);
                } else if(size1 == 4 || size2 == 4){
                    if(size1 == 4) dfs(0,2,processors_1,new ArrayList<Integer>(),result);
                    if(size2 == 4) dfs(0,2,processors_2,new ArrayList<Integer>(),result);
                } else if(size1 == 3 || size2 == 3){
                    if(size1 == 3) dfs(0,2,processors_1,new ArrayList<Integer>(),result);
                    if(size2 == 3) dfs(0,2,processors_2,new ArrayList<Integer>(),result);
                }
                break;
            case 4:
                if (size1 == 4 || size2 == 4) {
                    if(size1 == 4) result.add(processors_1);
                    if(size2 == 4) result.add(processors_2);
                }
                break;
            case 8:
                if (size1 == 4 && size2 == 4) {
                    result.add(processors_1);
                    result.add(processors_2);
                }
                break;
        }
        System.out.println(result.toString());
    }

    public static void dfs(int index, int level, ArrayList<Integer> processor,
                           ArrayList<Integer> path, ArrayList<ArrayList<Integer>> result) {
        if(path.size() == level) {
            result.add((ArrayList<Integer>) path.clone());
            return;
        }
        for (int i = index; i < processor.size(); i++) {
            path.add(processor.get(i));
            dfs(i + 1, level, processor, path, result);
            path.remove(path.size() - 1);
        }
    }
}
